---
import type { InternalFramework } from '@ag-grid-types';
import FrameworkTemplate from '@components/example-runner/components/FrameworkTemplate.astro';
import { getExampleUrl } from '../utils/urlPaths';
import { getGeneratedContents } from '@components/example-generator';

interface Props {
    internalFramework: InternalFramework;
    pageName: string;
    exampleName: string;
    /**
     * Whether to use relative paths for script files references
     */
    relativePath?: boolean;
    /**
     * Whether to add a script to post an `init` message to the parent
     */
    addInitMessageScript?: boolean;
    /**
     * Whether to not include system js code
     */
    ignoreSystemJs?: boolean;
    /**
     * Extra styles to add to HTML
     */
    extraStyles?: string;
}

const { internalFramework, pageName, exampleName, relativePath, addInitMessageScript, ignoreSystemJs, extraStyles } =
    Astro.props as Props;

const contentParams = {
    framework: internalFramework,
    pageName,
    exampleName,
};
let generatedContents;
try {
    generatedContents = await getGeneratedContents({
        type: 'docs',
        ...contentParams,
    });
} catch (error) {
    console.error('Error generating content for:', contentParams);
    console.error(error);
}

const exampleUrl = getExampleUrl({
    internalFramework,
    pageName,
    exampleName,
});
---

{!generatedContents && <p>Error generating content for: {JSON.stringify(contentParams)}</p>}
{
    generatedContents && (
        <FrameworkTemplate
            internalFramework={internalFramework}
            pageName={pageName}
            exampleName={exampleName}
            exampleUrl={exampleUrl}
            generatedContents={generatedContents!}
            relativePath={relativePath}
            addInitMessageScript={addInitMessageScript}
            ignoreSystemJs={ignoreSystemJs}
            extraStyles={extraStyles}
            nonce={generatedContents?.scriptNonce}
        />
    )
}
